package org.freedoit.server.dao;

// Generated Oct 11, 2012 3:54:06 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import org.freedoit.server.om.Task;
import org.freedoit.shared.exception.ServerException;


/**
 * Home object for domain model class Task.
 * @see org.freedoit.om.Task
 * @author Hibernate Tools
 */
public class TaskDAO {

	private static final Logger log = Logger.getLogger(TaskDAO.class.getName());

	private final PersistenceManagerFactory pmFactory = DAOFactory.getPersistenceManagerFactory();

	public void persist(Task task) {
		log.config("persisting Task instance");
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			pm.makePersistent(task);
			log.info("persist successful");
		} catch (RuntimeException re) {
			log.severe("persist failed:" + re.getMessage());
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	public void delete(Task task) {
		//log.debug("deleting Task instance");
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			pm.currentTransaction().begin();
			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			task = pm.getObjectById(Task.class, task.getTaskId());
			pm.deletePersistent(task);

			pm.currentTransaction().commit();
			//log.debug("delete successful");
		} catch (RuntimeException re) {
			//log.error("delete failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	public void update(Task task) {
		//log.debug("updating Task instance");
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			pm.currentTransaction().begin();
			pm.makePersistent(task);
			pm.currentTransaction().commit();
			//log.debug("update successful");
		} catch (RuntimeException re) {
			//log.error("update failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	public Task findById(Long id) {
		//log.debug("getting Task instance with id: " + id);
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			Task instance = pm.getObjectById(Task.class, id);
			if (instance == null) {
				//log.debug("get successful, no instance found");
			} else {
				//log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			//log.error("get failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Task> findByUserId(Long userId) {
		//log.debug("finding Task instances by user id:" + userId);
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("select from ").append(Task.class.getName());
			sbQuery.append(" where userId == ").append(userId);
			List<Task> results = (List<Task>) pm.newQuery(sbQuery.toString()).execute();
			//log.debug("find by user id successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			//log.error("find by user id failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Task> findByUserIdAndTimestamp(Long userId, Long timestamp) {
		//log.debug("finding Task instances by user id and timestamp:" + userId + " " + timestamp);
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("select from ").append(Task.class.getName());
			sbQuery.append(" where userId == ").append(userId);
			sbQuery.append(" && lastModifyTime > ").append(timestamp);
			List<Task> results = (List<Task>) pm.newQuery(sbQuery.toString()).execute();
			//log.debug("find by user id and timestamp successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			//log.error("find by user id and timestamp failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}
}
